/*
 * TimetablePanel.java
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.admin.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.TableModel;
import org.fpdev.core.transit.SubRoute;
import org.fpdev.core.transit.Timetable;
import org.fpdev.apps.admin.AdminClient;
import org.fpdev.core.transit.CalendarService;
import org.fpdev.util.gui.GUIFactory;

public class TimetablePanel extends JPanel implements ActionListener {

  private AdminClient av_;
  //private JTable table_;
  //private JComboBox day_;
  private JTabbedPane tabbedPane_;
  private JRadioButton tptOnlyRBtn_,  allStopsRBtn_;

  private List<CalendarService> services_;
  private CalendarService lastService_;
  
  private boolean nonTabBasedChange_;

  public TimetablePanel(AdminClient cv) {
    setLayout(new BorderLayout());

    av_ = cv;

    tabbedPane_ = new JTabbedPane();
    tabbedPane_.addChangeListener(new ChangeListener() {

      @Override
      public void stateChanged(ChangeEvent e) {

        if(nonTabBasedChange_) {
          nonTabBasedChange_ = false;
          return;
        }
        if(tabbedPane_.getSelectedIndex() == -1) {
          nonTabBasedChange_ = true;
          return;
        }
        lastService_ = services_.get(tabbedPane_.getSelectedIndex());
      }
    });
    nonTabBasedChange_ = false;

    tptOnlyRBtn_ = new JRadioButton("Show Timepoints Only", true);
    tptOnlyRBtn_.addActionListener(this);
    allStopsRBtn_ = new JRadioButton("Show All Stops");
    allStopsRBtn_.addActionListener(this);

    JPanel topRow = GUIFactory.newRowPanel();
    topRow.add(tptOnlyRBtn_);
    topRow.add(allStopsRBtn_);

    ButtonGroup btnGroup = new ButtonGroup();
    btnGroup.add(tptOnlyRBtn_);
    btnGroup.add(allStopsRBtn_);

    add(tabbedPane_, BorderLayout.CENTER);
    add(topRow, BorderLayout.NORTH);

  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == tptOnlyRBtn_ || e.getSource() == allStopsRBtn_) {
      av_.getRouteOps().updateTimetableInGUI();
    }
  }

  public void updateSubRoute(SubRoute sub) {
    tabbedPane_.removeAll();
    if(sub.timeTableCount() == 0) return;
    int toSelect = 0, i = 0;
    services_ = new ArrayList<CalendarService>();
    for(Timetable tt : sub.getTables()) {
      TableModel tm = sub.getTableModel(tt.getService(), getIncludeAllStops());
      services_.add(tt.getService());
      if(tt.getService() == lastService_) toSelect = i;
      JTable table = new JTable(tm);
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      tabbedPane_.add(tt.getService().getName(), new JScrollPane(table));
      i++;
    }
    tabbedPane_.setSelectedIndex(toSelect);
  }

  public boolean getIncludeAllStops() {
    return allStopsRBtn_.isSelected();
  }
}





























